//
//  CJPScanController.swift
//  CJPack
//
//  Created by 陈警卫 on 2024-02-02.
//

import UIKit
import AVFoundation

class CJPScanController: CJBaseBackController {

    let session = AVCaptureSession()
    var rectView: UIView!
    var previewLayer: AVCaptureVideoPreviewLayer!
    
    let scanImage = CJScanImage()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        var config = CJScanConfig()
        config.rectOffsetY = -100
        config.rectStyle = .corner
        config.showInterestRect = true
        let scanView = CJScanView(frame: CGRect(x: 0, y: CJScreen.navBatBarHeight,
                                                width: view.cj_width,
                                                height: view.cj_height - CJScreen.navBatBarHeight), config: config)
        view.addSubview(scanView)
        
        scanView.scanResultBlock { scaners, index, scaner in
            let value = scaner.message
            CJAlerter.showAlert(title: "扫描结果", message: value, cancelTitle: "取消", otherTitles: ["重新扫描"]) { [weak self] _, index in
                if index == 0 {
                    self?.navigationController?.popViewController(animated: true)
                } else if index == 1 {
                    scanView.startRunning()
                }
            }
        }
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "相册", style: .plain, target: self, action: #selector(selectLibrary))
    }
    
    @objc func selectLibrary() {
        
        scanImage.showLibraryQRCode(vc: self) { success, value in
            kCJLog(value)
        }
    }
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
    
}

/**
 let imageSize = image.size
 //CGSize(width: image.size.width / 3.0, height: image.size.height / 3.0)
 
 var transform = CGAffineTransform.identity
 transform = transform.scaledBy(x: 1, y: -1)
 transform = transform.translatedBy(x: 0, y: -imageSize.height)

 // 计算缩放比例，展示宽度(屏幕宽度) / 图片实际宽度
 let scaleX = view.bounds.size.width / imageSize.width
 let scaleY = scaleX

 // 得到要缩放的 transform
 let scaleTransform = CGAffineTransform(scaleX: scaleX, y: scaleY)
 
 let offsetY = (CJScreen.height - imageSize.height * scaleY) / 2.0
 
 var frame = qrFeature.bounds.applying(transform)
 frame = frame.applying(scaleTransform)
 frame.origin.y += offsetY
 
 let markv = UIView(frame: frame)
 markv.backgroundColor = .cyan
 markv.alpha = 0.5
 
 
 let markv = UIImageView(image: UIImage(named: "cj_scan_sel", in: .cj_bundle, compatibleWith: nil))
 
 let imageSize = image.size
 let viewSize = imageV.bounds.size
 
 let transform = imageV.transformForContentMode(imageSize: imageSize, viewSize: viewSize)
 let bounds = qrFeature.bounds.applying(transform)
 
 markv.cj_size = CGSize(width: 40, height: 40)
 markv.cj_centerX = bounds.origin.x + bounds.size.width * 0.5
 markv.cj_centerY = bounds.origin.y + bounds.size.height * 0.5
 
 //markv.bounds = bounds;
 view.addSubview(markv)
 
 
 extension UIImageView {
     func transformForContentMode(imageSize: CGSize, viewSize: CGSize) -> CGAffineTransform {
         switch contentMode {
         case .scaleAspectFit:
             let scale = min(viewSize.width / imageSize.width, viewSize.height / imageSize.height)
             let offsetX = (viewSize.width - imageSize.width * scale) / 2.0
             let offsetY = (viewSize.height - imageSize.height * scale) / 2.0
             return CGAffineTransform(scaleX: scale, y: scale).translatedBy(x: offsetX, y: offsetY)
         case .scaleAspectFill:
             let scale = max(viewSize.width / imageSize.width, viewSize.height / imageSize.height)
             let offsetX = (viewSize.width - imageSize.width * scale) / 2.0
             let offsetY = (viewSize.height - imageSize.height * scale) / 2.0
             return CGAffineTransform(scaleX: scale, y: scale).translatedBy(x: offsetX, y: offsetY)
         default:
             return .identity
         }
     }
 }
 */
